<?php
/*
 * The MIT License
 *
 * Copyright (c) 2010 http://www.bxdframework.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * Plugin per il routing degli url per il SEO.
 *
 * @author Fabrizio Filieri
 */
class Router extends BXDPlugin implements EventListener
{
	private $rule;


	public function __construct($rule)
	{
		$this->rule = $rule;
	}

	public function init()
	{
		EventManager::getInstance()->addEventListener(Event::$ON_BEFORE_PARSING_URL, $this);
	}

	public function deinit()
	{
		
	}

	public function getName()
	{
		return "RouterPlugin";
	}

	public function onAction($paramsEvent)
	{
		$url = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
		$url_token = explode("/", $url);
		$url_token_num = count($url_token);
		//var_dump($url_token);

		foreach ($this->rule as $rule => $route)
		{
			$route_token = explode("/", substr($rule, 1));
			//var_dump($route_token);
			//echo $rule ." = ".$url." => ".$route."<br>";

			// controllo numero dei token uguali
			if (count($route_token) != $url_token_num)
				continue;

			// controllo url con match 1:1
			if ($rule == "/".$url)
			{
				BXD::getInstance()->setNewRoute(substr($route['route'], 1));
				
				if (isset($route['http_status']))
				{
					BXD::loadLib('http/Http');
					Http::httpStatus($route['http_status']);
				}
				return;
			}

			$params = array();

			// parsing della regola
			$i = 0;
			foreach ($route_token as $token)
			{
				//echo "-$token = ".$url_token[$i]."<br/>";

				if (strpos($token, ":")===0) // è un token parametro
				{
					$params[$token] = $url_token[$i];
				}
				else
				{
					if ($token != $url_token[$i])
						continue;
				}

				$i++;
			}


			if ($i == $url_token_num) // il numero dei token trovati è lo stesso
			{
				foreach ($params as $key => $value)
				{
					$route['route'] = str_replace($key, $value, $route['route']);
				}

				BXD::getInstance()->setNewRoute(substr($route['route'], 1));

				if (isset($route['http_status']))
				{
					BXD::loadLib('http/Http');
					Http::httpStatus($route['http_status']);
				}

				return;
			}
		}
	}

}

?>
